package com.qcxy.gzy.app;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;
import java.io.IOException;

import android_serialport_api.SerialPort;

/**
 * 在所有的Activity中调用实现串口打开和关闭
 *
 * @author gzy
 */
public class ComApplication extends MultiDexApplication {
    private String TAG = getClass().getSimpleName();

    // public static String mPortPath = "/dev/ttyS0";
    // public static String mPortPath = "/dev/ttyS2";
    public static String mPortPath = "/dev/ttyS3";
    /**
     * 波特率
     */
    public static int mBaudRate = 19200;

    private SerialPort mSerialPort = null;
    /**
     * 记录当前的Activity
     */
    private String currentActivityName;

    private static ComApplication application;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        CrashReport.initCrashReport(getApplicationContext(), "9db1b3ad6f", true);
        //Thread.setDefaultUncaughtExceptionHandler(CrashExceptions.getInstance(this));
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
    }

    // 返回
    public static Context getContextObject() {
        return application.getApplicationContext();
    }

    /**
     * 获取设备mac地址
     */
    public static String getLocalMacAddressFromWifiInfo() {
        WifiManager wifi = (WifiManager) getContextObject().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    /**
     * 获得串口对象
     */
    public SerialPort getSerialPortInstance() {

        if (mSerialPort == null) {
            try {
                mSerialPort = new SerialPort(new File(mPortPath), mBaudRate, 0);
            } catch (SecurityException e) {
                e.printStackTrace();
                Log.d(TAG, "getSerialPortInstance:" + e.toString());
            } catch (IOException e) {
                e.printStackTrace();
                Log.d(TAG, "getSerialPortInstance:" + e.toString());
            }
        }
        return mSerialPort;
    }

    public String getCurrentActivityName() {
        return currentActivityName;
    }

    public void setCurrentActivityName(String currentActivityName) {
        this.currentActivityName = currentActivityName;
    }
}